<?php namespace Common\Model;
use Hdphp\Model\Model;
class Password extends Model{
	//指定表名
	protected $table='user';
	//自动完成
	protected $auto=[
		['password','md5','function',3,3]
	];
	//自动验证
	protected $validate=[
		['code','required','验证码不能为空',3,3],
		['code','_code','验证码输入错误',3,3],
		['oldpassword','required','旧密码不能为空',3,3]	,
		['password','required','新密码不能为空',3,3],
		['password','minlen:6;','新密码不能少于6位',3,3],
		['repassword','required','确认密码不能为空',3,3],
		['repassword','_repassword','两次密码输入不一致',3,3],
		['oldpassword','_oldpassword','旧密码输入错误',3,3]	,
		['password','_pwd','不能与旧密码一致',3,3]	
	];
	//自定义验证
	//验证码
	public function _code(){
	    $code=Q('post.code');
		if(strtolower($code)!=strtolower($_SESSION['code'])){
			return FALSE;
		}
		return TRUE;
	}
	//确认两次密码
	public function _repassword(){
	   	$password=Q('post.password','','md5');
	   	$repassword=Q('post.repassword','','md5');
		if($repassword!=$password){
			return FALSE;
		}
		return TRUE;
	}
	//确认旧密码正确
	public function _oldpassword(){
	   	$oldpassword=Q('post.oldpassword','','md5');
		$uid=Q('post.uid',0,'intval');
		$pwd=$this->where("uid={$uid}")->find();
		if($oldpassword!=$pwd['password']){
			return FALSE;
		}
		return TRUE;
	}
	//确认新旧密码是否一致
	public function _pwd(){
	    $oldpassword=Q('post.oldpassword','','md5');
		$password=Q('post.password','','md5');
		if($oldpassword==$password){
			return FALSE;
		}
		return TRUE;
	}
	//修改密码
	public function store(){
	    if(!$this->create()) return FALSE;
		return $this->save();
	}
}
 ?>